package com.inspur.gs.sbssdl.dzyhpgl.dzyhyu.dzyhyu.back.commoncmps;

import com.inspur.edp.bef.api.BefRtBeanUtil;
import com.inspur.edp.bef.api.lcp.IStandardLcp;
import com.inspur.edp.cef.entity.changeset.ModifyChangeDetail;
import com.inspur.edp.cef.entity.entity.IEntityData;
import com.inspur.edp.dxc.common.domain.CommonResult;
import com.inspur.edp.dxc.spi.components.BaseAfterDBUpdateComponent;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

public class DzyhpDrAfter extends BaseAfterDBUpdateComponent {

    // 库存表ID
    private String kcCfg = "com.inspur.gs.sbssdl.dzyhpgl.dzyhyu.dzyhyu.back.sbssYhpItem";

    public void importAfter(){
        CommonResult result = super.getResult();

        IStandardLcp kcLcp = BefRtBeanUtil.getLcpFactory().createLcp(kcCfg);

        List<ModifyChangeDetail> changeDetails = result.getChangeDetails();

        changeDetails.forEach(i -> {
            IEntityData data = kcLcp.retrieve(i.getID()).getData();

            BigDecimal sl = new BigDecimal(i.getPropertyChanges().get("tsl").toString());
            BigDecimal wlDj = (BigDecimal) i.getPropertyChanges().get("wlDj");

            ModifyChangeDetail changeDetail = new ModifyChangeDetail(i.getID());

            changeDetail.getPropertyChanges().put("zj",wlDj.multiply(sl));
            changeDetail.getPropertyChanges().put("fromType","1");
            changeDetail.getPropertyChanges().put("sysl", i.getPropertyChanges().get("tsl"));

            ArrayList<String> nodeCodes = new ArrayList<>();
            nodeCodes.add("itemtras");
            ArrayList<String> hierarchyIds = new ArrayList<>();
            hierarchyIds.add(data.getID());
            IEntityData kcChild = kcLcp.retrieveDefaultChild(nodeCodes, hierarchyIds);

            ModifyChangeDetail mxDetail = new ModifyChangeDetail(kcChild.getID());
            mxDetail.getPropertyChanges().put("trasType","3");
            mxDetail.getPropertyChanges().put("omission","0");
            mxDetail.getPropertyChanges().put("djid",i.getPropertyChanges().get("lydCode"));
            mxDetail.getPropertyChanges().put("trasSl",i.getPropertyChanges().get("tsl"));

            changeDetail.addChildChangeSet("itemtras",mxDetail);
            kcLcp.modify(changeDetail);
        });

        kcLcp.save();
        boolean executeResult = result.isSuccess();
        result.setSuccess(executeResult);
    }
}